//
//  SecondZhuangXiuViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-21.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "SecondZhuangXiuViewController.h"
#import "SecondCell.h"

@interface SecondZhuangXiuViewController ()
{
    UITableView *mainTable;
    NSMutableArray *unitLabels;
}

@end
@implementation SecondZhuangXiuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"二次装修查看";
    [self showMainView];
    NSURL *queryUrl = [NSURL URLWithString:querySecond];
    ASIFormDataRequest *obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:@"" forKey:@""];
    [obtainFloor startSynchronous];
    
    _floorNames = [NSMutableArray array];
    _decorationDatas = [NSMutableArray array];
    
    NSString *response = [obtainFloor responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];

    NSLog(@"查询%@",floorData);
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        [_floorNames addObject:[queryDic objectForKey:@"floorName"]];
        [_decorationDatas addObject:[queryDic objectForKey:@"decorationData"]];
    }
}
- (void)showMainView
{
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 90, whiteimage.size.width, whiteimage.size.height + 80)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, 40 + (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height + 40);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    UILabel *floorLb = [UILabel new];
    floorLb.text = @"楼层";
    floorLb.font = [UIFont systemFontOfSize:30];
    CGSize mysz = [floorLb.text sizeWithFont:floorLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    floorLb.frame = CGRectMake(lineView.frame.origin.x + 20, (lineView.frame.origin.y - mysz.height)/2, mysz.width, mysz.height);
    [MyView addSubview:floorLb];
    
    unitLabels = [NSMutableArray array];

    for (int i = 0; i < 6; i ++)
    {
        UILabel *floorLbNew = [UILabel new];
        floorLbNew.text = [NSString stringWithFormat:@"%02d单元",i+1];
        floorLbNew.font = [UIFont systemFontOfSize:30];
        mysz = [floorLbNew.text sizeWithFont:floorLbNew.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        floorLbNew.frame = CGRectMake((MyView.frame.size.width - 40 - floorLb.frame.size.width)/6*i+((lineView.frame.size.width - 40 - floorLb.frame.size.width)/6-mysz.width)/2 + floorLb.frame.origin.x + floorLb.frame.size.width, floorLb.frame.origin.y,(MyView.frame.size.width - 40 - floorLb.frame.size.width)/6, mysz.height);
        floorLbNew.textAlignment = 1;
        [MyView addSubview:floorLbNew];
        [unitLabels addObject:floorLbNew];
    }
    
    mainTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 2, lineImage.size.width-6, lineImage.size.height+20) style:UITableViewStylePlain];
    mainTable.dataSource = self;
    mainTable.delegate = self;
    [lineView addSubview:mainTable];
    mainTable.tag = 100;
 
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _floorNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];
    SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.delegate = self;
    if (!cell)
    {
        NSInteger officeNum = 0;
        cell = [[SecondCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        NSArray *decorationArr = [_decorationDatas objectAtIndex:indexPath.row];
        for (int i = 0; i < decorationArr.count; i ++)
        {
            NSDictionary *decorationDic = [decorationArr objectAtIndex:i];
            NSString *officeId = [decorationDic objectForKey:@"officeId"];
            NSArray *officeNames = [decorationDic objectForKey:@"officeName"];
            for (int j = 0; j < officeNames.count; j++)
            {
                if (j >= officeNum)
                {
                    officeNum = j;
                }
                NSDictionary *officeInfoDic = [officeNames objectAtIndex:j];
                NSString *officeNameAndTelephone = [officeInfoDic objectForKey:@"officeName"];
                NSString *officeName = @"";
                NSString *officeTelephone = @"";
                if ([officeNameAndTelephone rangeOfString:@"@#$"].location != NSNotFound)
                {
                    NSArray *stringArr = [officeNameAndTelephone componentsSeparatedByString:@"@#$"];
                    officeName = [stringArr objectAtIndex:0];
                    officeTelephone = [stringArr objectAtIndex:1];
                }
                else
                {
                    officeName = officeNameAndTelephone;
                }
                NSString *isrenvoation = [officeInfoDic objectForKey:@"isrenvoation"];
                NSString *outside = [officeInfoDic objectForKey:@"outside"];
                
                UILabel *officeNameLb = [UILabel new];
                officeNameLb.text = officeName;
                officeNameLb.font = [UIFont systemFontOfSize:14];
                CGSize mySz = [officeNameLb.text sizeWithFont:officeNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                if ([isrenvoation integerValue] == 1)
                {
                    officeNameLb.textColor = [UIColor redColor];
                }
                officeNameLb.numberOfLines = 0;
                officeNameLb.frame = CGRectMake([[unitLabels objectAtIndex:[officeId integerValue]-1]frame].origin.x - 15, 3 + (officeNameLb.frame.size.height + 55)*j, [[unitLabels objectAtIndex:[officeId integerValue]-1]frame].size.width, mySz.height);
                officeNameLb.textAlignment = 1;
                [cell addSubview:officeNameLb];
                
                if ([officeTelephone isEqualToString:@""])
                {

                }
                else
                {
                    UILabel *officeTeleLb = [UILabel new];
                    if ([outside integerValue] == 0)
                    {
                        officeTeleLb.text = [NSString stringWithFormat:@"* %@",officeTelephone];
                    }
                    else
                    {
                        officeTeleLb.text = officeTelephone;
                    }
                    officeTeleLb.font = [UIFont systemFontOfSize:15];
                    mySz = [officeTeleLb.text sizeWithFont:officeTeleLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
                    officeTeleLb.frame = CGRectMake(officeNameLb.frame.origin.x, officeNameLb.frame.origin.y + officeNameLb.frame.size.height+10, officeNameLb.frame.size.width, mySz.height);
                    officeTeleLb.textAlignment = 1;
                    [cell addSubview:officeTeleLb];
                }
                cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 66*(officeNum+1));
            }
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.floorNameLb.text = [_floorNames objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
